/**
 * 运动员注册表单配置
 * 基于QForm组件的配置化表单
 */

import { ref } from 'vue'
import type { FormItemRows } from '@meetjs/element-plus-kit'
import { useDict } from '@/composables/useDict'
import { GRADE_OPTIONS, SYSTEM_DICT, ATHLETE_DICT } from '@/constants'

// 使用 useDict hook 获取字典和组织级联数据
const { dicts, registerOrgOptions, orgCascadeOptions } = useDict([SYSTEM_DICT.SEX, ATHLETE_DICT.PROJECT, SYSTEM_DICT.NATION])

/**
 * 基本信息表单配置
 */
export const basicFormOptions = ref<FormItemRows[]>([
	// 第一行：姓名 + 民族
	{
		type: 'input',
		label: '姓名',
		prop: 'athleteName',
		required: true,
		col: 12,

		attrs: {
			clearable: true,
			maxlength: 20
		}
	},
	{
		type: 'select',
		label: '民族',
		prop: 'nation',
		required: true,
		col: 12,
		options: dicts[SYSTEM_DICT.NATION] as unknown as any
	},

	// 第二行：性别 + 出生日期
	{
		type: 'select',
		label: '性别',
		prop: 'sex',
		required: true,
		col: 12,
		options: dicts[SYSTEM_DICT.SEX] as unknown as any
	},
	{
		type: 'date-picker',
		label: '出生日期',
		prop: 'birthday',
		required: true,
		col: 12,
		attrs: {
			type: 'date',
			placeholder: '年/月/日',
			format: 'YYYY/MM/DD',
			'value-format': 'YYYY-MM-DD',
			'disabled-date': (time: Date) => {
				// 限制不能选择未来日期和过于久远的日期
				const today = new Date()
				const minDate = new Date()
				minDate.setFullYear(today.getFullYear() - 50)
				return time.getTime() > today.getTime() || time.getTime() < minDate.getTime()
			}
		}
	},

	// 第三行：证件号码 + 发证机关
	{
		type: 'input',
		label: '证件号码',
		prop: 'idNumber',
		required: true,
		col: 12,
		attrs: {
			placeholder: '请输入证件号码',
			maxlength: 18
		},
		formItem: {
			rules: [
				{
					required: true,
					message: '请输入证件号码'
				},
				{
					pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
					message: '证件号码格式不正确'
				}
			]
		}
	},
	{
		type: 'input',
		label: '发证机关',
		prop: 'authority',
		required: true,
		col: 12,
		attrs: {
			placeholder: '请输入发证机关',
			clearable: true,
			maxlength: 50
		}
	},

	// 第四行：有效期起始时间 + 有效期截止时间
	{
		type: 'date-picker',
		label: '有效期起始时间',
		prop: 'cardExpStart',
		required: true,
		col: 12,
		attrs: {
			type: 'date',
			placeholder: '请输入有效期起止时间',
			format: 'YYYY-MM-DD',
			'value-format': 'YYYY-MM-DD'
		}
	},
	{
		type: 'date-picker',
		label: '有效期截止时间',
		prop: 'cardExpEnd',
		required: true,
		col: 12,
		attrs: {
			type: 'date',
			placeholder: '请输入有效期截止时间',
			format: 'YYYY-MM-DD',
			'value-format': 'YYYY-MM-DD'
		}
	},

	// 第五行：户籍地址 + 身高
	{
		type: 'input',
		label: '户籍地址',
		prop: 'address',
		required: true,
		col: 12,
		attrs: {
			placeholder: '请输入详细地址',
			clearable: true,
			maxlength: 200
		}
	},

	{
		type: 'input-number',
		label: '身高（cm）',
		prop: 'height',
		required: true,
		col: 12,
		attrs: {
			placeholder: '请输入身高',
			min: 100,
			max: 250,
			controls: false,
			'controls-position': 'right'
		}
	},

	// 第六行：体重 + 注册证号
	{
		type: 'input-number',
		label: '体重（kg）',
		prop: 'weight',
		required: true,
		col: 12,
		attrs: {
			placeholder: '请输入体重',
			min: 20,
			max: 200,
			controls: false,
			'controls-position': 'right'
		}
	},
	{
		type: 'input',
		label: '注册证号',
		prop: 'registerNumber',
		required: false,
		col: 12,
		attrs: {
			placeholder: '注册成功后自动生成',
			disabled: true
		}
	},

	// 第七行：注册单位 + 在训单位
	{
		type: 'select',
		label: '注册单位',
		prop: 'registerOrgId',
		required: true,
		col: 12,
		options: registerOrgOptions as unknown as any
	},
	{
		type: 'cascader',
		label: '在训单位',
		prop: 'trainOrgCascade',
		required: true,
		col: 12,
		options: orgCascadeOptions as unknown as any,
		attrs: {
			'show-all-levels': false
		}
	},

	// 第八行：在读学校 + 在读年级
	{
		type: 'input',
		label: '在读学校',
		prop: 'school',
		required: true,
		col: 12,
		attrs: {
			placeholder: '请输入在读学校',
			clearable: true,
			maxlength: 20
		}
	},
	{
		type: 'select',
		label: '在读年级',
		prop: 'grade',
		required: true,
		col: 12,
		options: GRADE_OPTIONS
	},

	// 第九行：学籍卡号 + 注册项目
	{
		type: 'input',
		label: '学籍卡号',
		prop: 'studentNumber',
		required: true,
		col: 12,
		attrs: {
			clearable: true,
			maxlength: 30
		}
	},
	{
		type: 'select',
		label: '注册项目',
		prop: 'registerProject',
		required: true,
		col: 12,
		options: dicts.comp_athlete_project as unknown as any
	},

	// 第十行：现任教练员 + 监护人
	{
		type: 'input',
		label: '现任教练员',
		prop: 'coachName',
		required: true,
		col: 12,
		attrs: {
			clearable: true,
			maxlength: 10
		}
	},

	{
		type: 'input',
		label: '监护人',
		prop: 'guardian',
		required: true,
		col: 12,
		attrs: {
			placeholder: '请输入监护人姓名',
			clearable: true,
			maxlength: 50
		}
	},

	// 第十一行：监护人联系方式 + 协议起止时间
	{
		type: 'input',
		label: '监护人联系方式',
		prop: 'guardianPhone',
		required: true,
		col: 12,
		attrs: {
			placeholder: '请输入监护人的联系方式',
			clearable: true,
			maxlength: 20
		},
		formItem: {
			rules: [
				{
					required: true,
					message: '请输入监护人联系方式'
				},
				{
					pattern: /^1[3-9]\d{9}$/,
					message: '请输入正确的手机号码'
				}
			]
		}
	},

	{
		type: 'date-picker',
		label: '协议起止时间',
		prop: 'agreementPeriod',
		required: true,
		col: 12,
		attrs: {
			type: 'daterange',
			'range-separator': '—',
			'start-placeholder': '开始日期',
			'end-placeholder': '结束日期',
			format: 'YYYY-MM-DD',
			'value-format': 'YYYY-MM-DD',
			onChange: (value: [string, string] | null) => {
				// 这里需要在组件中处理，将范围值分别设置到 agreementStart 和 agreementEnd
			}
		}
	},
	// 年份选择器
	{
		type: 'date-picker',
		label: '注册年度',
		prop: 'registerYear',
		required: true,
		col: 12,
		attrs: {
			type: 'year',
			format: 'YYYY',
			'value-format': 'YYYY'
		}
	}
])

/**
 * 单位变更信息表单配置
 */
export const unitChangeFormOptions = ref<FormItemRows[]>([
	{
		type: 'select',
		label: '变更前单位信息',
		prop: 'changeBefore',
		required: true,
		col: 12,
		options: registerOrgOptions as unknown as any,
		attrs: {
			clearable: true,
			disabled: true
		}
	},
	{
		type: 'select',
		label: '变更后单位信息',
		prop: 'changeAfter',
		required: true,
		col: 12,
		options: registerOrgOptions as unknown as any,
		attrs: {
			placeholder: '请选择变更后单位',
			clearable: true
		}
	}
])

/**
 * 项目变更信息表单配置
 */
export const projectChangeFormOptions = ref<FormItemRows[]>([
	{
		type: 'select',
		label: '变更前项目信息',
		prop: 'changeBefore',
		required: true,
		col: 12,
		options: dicts.comp_athlete_project as unknown as any,
		attrs: {
			clearable: true,
			disabled: true
		}
	},
	{
		type: 'select',
		label: '变更后项目信息',
		prop: 'changeAfter',
		required: true,
		col: 12,
		options: dicts.comp_athlete_project as unknown as any,
		attrs: {
			clearable: true
		}
	}
])

/**
 * 表单验证规则生成器
 */
export function createFormRules() {
	return {
		athleteName: [
			{
				required: true,
				message: '请输入姓名',
				trigger: 'blur'
			},
			{
				min: 2,
				max: 20,
				message: '姓名长度在2到20个字符',
				trigger: 'blur'
			}
		],
		nation: [
			{
				required: true,
				message: '请选择民族',
				trigger: 'change'
			}
		],
		sex: [
			{
				required: true,
				message: '请选择性别',
				trigger: 'change'
			}
		],
		birthday: [
			{
				required: true,
				message: '请选择出生日期',
				trigger: 'change'
			}
		],
		idNumber: [
			{
				required: true,
				message: '请输入证件号码',
				trigger: 'blur'
			},
			{
				pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
				message: '请输入正确的证件号码',
				trigger: 'blur'
			}
		],
		authority: [
			{
				required: true,
				message: '请输入发证机关',
				trigger: 'blur'
			}
		],
		cardExpStart: [
			{
				required: true,
				message: '请选择有效期起始时间',
				trigger: 'change'
			}
		],
		cardExpEnd: [
			{
				required: true,
				message: '请选择有效期截止时间',
				trigger: 'change'
			}
		],
		address: [
			{
				required: true,
				message: '请输入户籍地址',
				trigger: 'blur'
			},
			{
				max: 200,
				message: '地址不能超过200个字符',
				trigger: 'blur'
			}
		],
		height: [
			{
				required: true,
				message: '请输入身高',
				trigger: 'blur'
			},
			{
				type: 'number',
				min: 100,
				max: 250,
				message: '身高应在100-250cm之间',
				trigger: 'blur'
			}
		],
		weight: [
			{
				required: true,
				message: '请输入体重',
				trigger: 'blur'
			},
			{
				type: 'number',
				min: 20,
				max: 200,
				message: '体重应在20-200kg之间',
				trigger: 'blur'
			}
		],
		registerOrgId: [
			{
				required: true,
				message: '请选择注册单位',
				trigger: 'change'
			}
		],
		school: [
			{
				required: true,
				message: '请输入在读学校',
				trigger: 'blur'
			}
		],
		grade: [
			{
				required: true,
				message: '请选择在读年级',
				trigger: 'change'
			}
		],
		trainOrgId: [
			{
				required: true,
				message: '请选择在训单位',
				trigger: 'change'
			}
		],
		studentNumber: [
			{
				required: true,
				message: '请输入学籍卡号',
				trigger: 'blur'
			}
		],
		registerProject: [
			{
				required: true,
				message: '请选择注册项目',
				trigger: 'change'
			}
		],
		coachName: [
			{
				required: true,
				message: '请输入教练员姓名',
				trigger: 'blur'
			}
		],
		guardian: [
			{
				required: true,
				message: '请输入监护人姓名',
				trigger: 'blur'
			}
		],
		guardianPhone: [
			{
				required: true,
				message: '请输入监护人联系方式',
				trigger: 'blur'
			},
			{
				pattern: /^1[3-9]\d{9}$/,
				message: '请输入正确的手机号码',
				trigger: 'blur'
			}
		],
		agreementStart: [
			{
				required: true,
				message: '请选择协议起始时间',
				trigger: 'change'
			}
		]
	}
}
